/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package aso.upna.controllers;

import aso.upna.cancion.AbstractCancion;
import aso.upna.playlist.GenericIterator;
import aso.upna.playlist.PlayList;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author triki
 */

public class PlaylistTableController {

    private JTable ref_table;
    private PlayList ref_playlist;

    private final int DEF_ROWS = 14;

    public PlaylistTableController(JTable table)
    {
        ref_table = table;
        ref_playlist = null;
    }

    public void registerPlaylist(PlayList pl)
    {
        ref_playlist = pl;
    }

    public void unregisterPlaylist()
    {
        ref_playlist = null;
    }

    public void refresh()
    {
        GenericIterator iter;
        DefaultTableModel tab;
        Object obj[] = null;
        AbstractCancion can;

        int i,rows;

        if(ref_playlist == null)
            return;

        iter = ref_playlist.getIterator();
        tab = (DefaultTableModel) ref_table.getModel();
        i = 0;
        rows = ref_table.getRowCount();

        while(iter.hasNext())
        {
            if(i > rows-1)
            {
                tab.addRow(obj);
            }
            can = (AbstractCancion)iter.current();
            ref_table.setValueAt(i+"-", i, 0);
            if(can.hasMetadata())
            {
                ref_table.setValueAt(can.getMetadatos().getTitulo(),i,1);
                ref_table.setValueAt(can.getMetadatos().getGrupo(),i,2);
                ref_table.setValueAt(can.getMetadatos().getAlbum(),i,3);
            }
            else
            {
                ref_table.setValueAt(can.getFilename(),i,1);
            }
            
            ref_table.setValueAt(can.getDuracion(),i,4);
            i++;
            iter.next();
        }

    }

     private void cleanTable()
    {
        int rows,cols,i,j;

        DefaultTableModel tab = (DefaultTableModel) ref_table.getModel();
        rows = ref_table.getRowCount();
        cols = ref_table.getColumnCount();

        tab.setNumRows(DEF_ROWS);
        
        rows = ref_table.getRowCount();

        for(i=0;i<rows;i++)
        {
            for(j=0;j<cols;j++)
            {
                ref_table.setValueAt("", i, j);
            }
        }
    }
}
